package com.hezhiqin.utils;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: JGPushMessage
 *
 * @author hezhiqin
 * @Description: TODO 极光推送
 * @date 2017年12月28日
 */
public class JGPushMessage {

    private static final String secret = "43f16934de68afb0271c994c";
    private static final String appkey = "fe8b569a1638a8b07ea36a00";

    /**
     * @param @param  secret
     * @param @param  appkey
     * @param @param  msg_title
     * @param @param  notification_title
     * @param @param  tag
     * @param @param  msg_content
     * @param @param  contentType
     * @param @return
     * @return boolean
     * @throws
     * @Description: TODO 推送通知栏
     * @author hezhiqin
     * @date 2018年6月14日
     */
    public static boolean pushMessage(String msg_title, String notification_title, String[] tag, String msg_content, String contentType) {
        // ClientConfig clientConfig = ClientConfig.getInstance();
        JPushClient jpushClient = new JPushClient(secret, appkey);
        PushPayload payload =  JGPushMessage.buildPush_android_ios(tag, notification_title, msg_title, msg_content, contentType);
        try {
            PushResult result = jpushClient.sendPush(payload);
            Thread.sleep(1000);
            jpushClient.close();
            if (result.isResultOK()) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        } finally {
            // 请求结束后，调用 NettyHttpClient 中的 close 方法，否则进程不会退出。
            jpushClient.close();

        }

    }

    /**
     * @param @param  secret
     * @param @param  appkey
     * @param @param  msg_title
     * @param @param  tag
     * @param @param  msg_content
     * @param @param  contentType
     * @param @return
     * @return boolean
     * @throws
     * @Description: TODO 推送自定义消息
     * @author hezhiqin
     * @date 2018年6月14日
     */
    public static boolean pushMessage(String msg_title, String[] tag, String msg_content, String contentType) {
        // ClientConfig clientConfig = ClientConfig.getInstance();
        JPushClient jpushClient = new JPushClient(secret, appkey);
        PushPayload payload =  JGPushMessage.messagePayloadPush(tag, msg_title, msg_content, contentType);
        try {
            PushResult result = jpushClient.sendPush(payload);
            Thread.sleep(1000);
            jpushClient.close();
            if (result.isResultOK()) {
                return true;
            } else {
                return false;
            }
        } catch (APIConnectionException | APIRequestException | InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        } finally {
            // 请求结束后，调用 NettyHttpClient 中的 close 方法，否则进程不会退出。
            jpushClient.close();

        }

    }

    /**
     * 推送给所有人员
     *
     * @param content
     * @param alert
     * @param masterKey
     * @param appKey
     * @return
     */
    public static boolean pushMessageToAll(String content, String alert, String masterKey, String appKey) {

        JPushClient jpushClient = new JPushClient(masterKey, appKey);

        Map<String, String> contentMap = new HashMap<String, String>();
        contentMap.put("value", content);
        contentMap.put("catagory", "1");

        PushPayload iOSPayload = PushPayload.newBuilder().setOptions(Options.newBuilder().setApnsProduction(true).build()).setAudience(Audience.all()).setPlatform(Platform.ios())
                .setNotification(Notification.ios(alert, contentMap)).build();

        try {
            PushResult iOSResult = jpushClient.sendPush(iOSPayload);
            System.out.println("Got result - " + iOSResult);
            return true;
        } catch (APIConnectionException e) {
            System.out.println("Connection error, should retry later");
            return false;
        } catch (APIRequestException e) {
            System.out.println("Should review the error, and fix the request");
            System.out.println("HTTP Status: " + e.getStatus());
            System.out.println("Error Code: " + e.getErrorCode());
            System.out.println("Error Message: " + e.getErrorMessage());
            return false;
        }
    }

    /**
     * @param @param  tag
     * @param @param  notification_title
     * @param @param  msg_title
     * @param @param  msg_content
     * @param @param  extrasparam
     * @param @return
     * @return PushPayload
     * @throws
     * @Description: TODO
     * @author hezhiqin
     * @date 2017年12月28日
     */
    public static PushPayload buildPush_android_ios(String[] tag, String notification_title, String msg_title, String msg_content, String contentType) {
        boolean isApns = false;
        return PushPayload
                .newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.registrationId(tag))
                .setNotification(
                        Notification.newBuilder().setAlert(notification_title)
                                .addPlatformNotification(AndroidNotification.newBuilder().setAlert(notification_title).setTitle(notification_title).addExtra("extrasparam", msg_content).addExtra("type", contentType).build())
                                .addPlatformNotification(IosNotification.newBuilder().setAlert(notification_title).incrBadge(1).setSound("sound.caf").addExtra("extrasparam", msg_content).addExtra("type", contentType).setContentAvailable(true).build()).build())
                //.setMessage(Message.newBuilder().setMsgContent(msg_content).setTitle(msg_title).setContentType(contentType).build())
                .setOptions(Options.newBuilder().setApnsProduction(isApns).setSendno(1).setTimeToLive(86400).build()).build();
    }

    /**
     * @param @param  tag
     * @param @param  msg_title
     * @param @param  msg_content
     * @param @param  contentType
     * @param @return
     * @return PushPayload
     * @throws
     * @Description: TODO自定义消息体
     * @author hezhiqin
     * @date 2018年6月13日
     */
    public static PushPayload messagePayloadPush(String[] tag, String msg_title, String msg_content, String contentType) {
        boolean isApns = false;
        return PushPayload
                .newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(tag))
                .setMessage(Message.newBuilder().setMsgContent(msg_content).setTitle(msg_title).setContentType(contentType).build())
                .setOptions(Options.newBuilder().setApnsProduction(isApns).setSendno(1).setTimeToLive(86400).build()).build();
    }

    /**
     * @param string
     * @param notification_title
     * @param alarmAddress
     * @param toUserId
     * @param jsonString
     * @param type
     * @return
     * @Title: pushMessage
     * @Description: TODO
     * @return:boolean
     * @author hezhiqin
     * @date 2018年8月21日下午3:13:41
     */
    public static boolean pushMessage(String msg_title, String notification_title, String alarmAddress, String[] tag,
                                      String msg_content, String contentType) {
        JPushClient jpushClient = new JPushClient(secret, appkey);
        PushPayload payload =  JGPushMessage.buildPush_android_ios(tag, alarmAddress, notification_title, msg_title, msg_content, contentType);
        try {
            PushResult result = jpushClient.sendPush(payload);
            Thread.sleep(1000);
            jpushClient.close();
            if (result.isResultOK()) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        } finally {
            // 请求结束后，调用 NettyHttpClient 中的 close 方法，否则进程不会退出。
            jpushClient.close();

        }
    }

    /**
     * @param tag
     * @param alarmAddress
     * @param notification_title
     * @param msg_title
     * @param msg_content
     * @param contentType
     * @return
     * @Title: buildPush_android_ios
     * @Description: TODO
     * @return:PushPayload
     * @author hezhiqin
     * @date 2018年8月21日下午3:18:28
     */
    private static PushPayload buildPush_android_ios(String[] tag, String alarmAddress, String notification_title,
                                                     String msg_title, String msg_content, String contentType) {
        boolean isApns = false;
        return PushPayload
                .newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(tag))
                .setNotification(
                        Notification.newBuilder().setAlert(notification_title)
                                .addPlatformNotification(AndroidNotification.newBuilder().setAlert(alarmAddress).setTitle(notification_title).addExtra("extrasparam", msg_content).addExtra("type", contentType).build())
                                .addPlatformNotification(IosNotification.newBuilder().setAlert(notification_title).incrBadge(1).setSound("sound.caf").addExtra("extrasparam", msg_content).addExtra("type", contentType).setContentAvailable(true).build()).build())
                //.setMessage(Message.newBuilder().setMsgContent(msg_content).setTitle(msg_title).setContentType(contentType).build())
                .setOptions(Options.newBuilder().setApnsProduction(isApns).setSendno(1).setTimeToLive(86400).build()).build();
    }


}
